package com.proton.carepatchtemp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.database.table.FirmWare;
import com.proton.carepatchtemp.utils.AppUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

public class DeviceUpdateMsgActivity extends BaseActivity {

    private ImageButton mBtnBack;
    /**
     * 卡帕奇儿童体温贴
     */
    private TextView mTvDeviceName;
    /**
     * V 1.0
     */
    private TextView mTvDeviceNowVersion;
    /**
     * 没有更新
     */
    private TextView mTvNoUpdate;
    /**
     * 新版本固件 ： V1.1
     */
    private TextView mTvFirmwareUpdateVersion;
    /**
     * gnsgnisdngkfohgkaoffa\ngdasgsdgsagasg
     */
    private TextView mTvFirmwareUpdateMsg;
    private LinearLayout mLnlUpdateMsg;
    /**
     * 固件更新
     */
    private Button mBtGoFwUpdate;


    private Device mDevice;

    private FirmWare mFirmWare;

    @Override
    public void dealEvent(MainEvent event) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_update_msg);
        initView();
        setListener();
        if (getIntent().getExtras() == null) {
            finish();
        } else {
            mDevice = (Device) getIntent().getExtras().getSerializable(Extras.DEVICE_DETIAL);
        }
        if (null == mDevice) {
            finish();
        }

        initData();

    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(v -> {
            finish();
        });

        mBtGoFwUpdate.setOnClickListener(v -> {
            Intent goUpdateFW = new Intent(DeviceUpdateMsgActivity.this, FireWareUpdateActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Extras.DEVICE_DETIAL, mDevice);
            goUpdateFW.putExtras(bundle);
            startActivity(goUpdateFW);
        });

    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mTvDeviceName = findViewById(R.id.tv_device_name);
        mTvDeviceNowVersion = findViewById(R.id.tv_device_now_version);
        mTvNoUpdate = findViewById(R.id.tv_no_update);
        mTvFirmwareUpdateVersion = findViewById(R.id.tv_firmware_update_version);
        mTvFirmwareUpdateMsg = findViewById(R.id.tv_firmware_update_msg);
        mLnlUpdateMsg = findViewById(R.id.lnl_update_msg);
        mBtGoFwUpdate = findViewById(R.id.bt_go_fw_update);
    }


    private void initData() {
        mTvDeviceName.setText(mDevice.getName());
        mTvDeviceNowVersion.setText(mDevice.getVersion());
        if (null != VCare.get().getFirmWare()) {
            mFirmWare = VCare.get().getFirmWare();

            if (!TextUtils.isEmpty(mDevice.getVersion()) && AppUtils.compareVersion(mDevice.getVersion(), "1.0.2") >= 0 && AppUtils.compareVersion(mDevice.getVersion(), mFirmWare.getVersion()) < 0) {
                mTvNoUpdate.setVisibility(View.GONE);
                mTvNoUpdate.setVisibility(View.GONE);
                mTvFirmwareUpdateMsg.setText(mFirmWare.getContent());
                mTvFirmwareUpdateVersion.setText(String.format(getString(R.string.string_new_hardware), mFirmWare.getVersion()));
                mBtGoFwUpdate.setClickable(true);
            } else {
                mLnlUpdateMsg.setVisibility(View.INVISIBLE);
                mTvNoUpdate.setVisibility(View.VISIBLE);
                mBtGoFwUpdate.setTextColor(ContextCompat.getColor(DeviceUpdateMsgActivity.this, R.color.gray));
                mBtGoFwUpdate.setBackground(ContextCompat.getDrawable(DeviceUpdateMsgActivity.this, R.drawable.gray_radios4_null_bg));
                mBtGoFwUpdate.setClickable(false);
            }

        } else {
            mTvNoUpdate.setVisibility(View.VISIBLE);
            mBtGoFwUpdate.setTextColor(ContextCompat.getColor(DeviceUpdateMsgActivity.this, R.color.gray));
            mBtGoFwUpdate.setBackground(ContextCompat.getDrawable(DeviceUpdateMsgActivity.this, R.drawable.gray_radios4_null_bg));
            mBtGoFwUpdate.setClickable(false);
        }
    }

    @Override
    public void onEventMainThread(MainEvent event) {

    }

}
